package sunsun.xiaoli.jiarebang.adapter;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.itboye.pondteam.bean.DeviceListBean;
import org.json.JSONException;
import org.json.JSONObject;
import sunsun.xiaoli.jiarebang.R;

import java.util.ArrayList;


public class DeviceListForPondLinkAdapter extends BaseAdapter {
    ArrayList<DeviceListBean> deviceListBeanArrayList;
    Activity activity;

    public DeviceListForPondLinkAdapter(ArrayList<DeviceListBean> videoArrBeans, Activity activity) {
        this.deviceListBeanArrayList = videoArrBeans;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return deviceListBeanArrayList == null ? 0 : deviceListBeanArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(activity).inflate(
                    R.layout.device_item_pond, null);
            holder.textView_dev_name = convertView.findViewById(R.id.textView_dev_name);
            holder.textView_dev_did = convertView.findViewById(R.id.textView_dev_did);
            holder.textView_dev_psw = convertView.findViewById(R.id.textView_dev_psw);
            holder.imageView_dev_logo = convertView.findViewById(R.id.imageView_dev_logo);
            holder.textView_dev_state1 = convertView.findViewById(R.id.textView_dev_state1);
            holder.textView_dev_state2 = convertView.findViewById(R.id.textView_dev_state2);
            holder.imageView_dev_right_arrow = convertView.findViewById(R.id.imageView_dev_right_arrow);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DeviceListBean deviceListBean = deviceListBeanArrayList.get(position);
        holder.textView_dev_name.setText(deviceListBean.getDevice_nickname());
        holder.textView_dev_name.setSelected(true);
        holder.textView_dev_did.setText("DID:" + deviceListBean.getDid());
        try {
            JSONObject jsonObject = new JSONObject(deviceListBean.getExtra());
            if (jsonObject.has("pwd")) {
                holder.textView_dev_psw.setText("PSW:" + jsonObject.getString("pwd"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        holder.imageView_dev_logo.setImageResource(getResourceValue(deviceListBean.getIs_disconnect(), deviceListBean.getDevice_type(), position));
        holder.textView_dev_state1.setVisibility(View.GONE);
        holder.textView_dev_state2.setVisibility(View.GONE);
        return convertView;
    }

    private String getConnectStatus(boolean is_disconnect) {
        return (is_disconnect ? "<font color='" + ContextCompat.getColor(activity, R.color.main_green) + "'>" + activity.getString(R.string.device) + activity.getString(R.string.online) + "</font>" : "<font color='gray'>" + activity.getString(R.string.device) + activity.getString(R.string.offline) + "</font>");
    }

    private int getResourceValue(int is_disconnect, String device_type, int position) {
//        if (device_type.startsWith("S01")) {
        //过滤桶
        return R.drawable.pondteam;
//        }
    }

    class ViewHolder {
        TextView textView_dev_name, textView_dev_did, textView_dev_state1, textView_dev_state2, textView_dev_psw;
        ImageView imageView_dev_logo, imageView_dev_right_arrow;
    }


}
